library(ggplot2)
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
## ✓ tibble  3.1.6     ✓ dplyr   1.0.8
## ✓ tidyr   1.2.0     ✓ stringr 1.4.0
## ✓ readr   2.1.2     ✓ forcats 0.5.1
## ✓ purrr   0.3.4
## Warning: package 'tidyr' was built under R version 4.1.2
## Warning: package 'readr' was built under R version 4.1.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(zoo)
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
library(dplyr)
library(lubridate)
## 
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(devtools)
## Loading required package: usethis
library(leaflet)
## Warning: package 'leaflet' was built under R version 4.1.2
library(readr)
library(rgdal)
## Loading required package: sp
## Please note that rgdal will be retired by the end of 2023,
## plan transition to sf/stars/terra functions using GDAL and PROJ
## at your earliest convenience.
## 
## rgdal: version: 1.5-28, (SVN revision 1158)
## Geospatial Data Abstraction Library extensions to R successfully loaded
## Loaded GDAL runtime: GDAL 3.2.1, released 2020/12/29
## Path to GDAL shared files: /Library/Frameworks/R.framework/Versions/4.1/Resources/library/rgdal/gdal
## GDAL binary built with GEOS: TRUE 
## Loaded PROJ runtime: Rel. 7.2.1, January 1st, 2021, [PJ_VERSION: 721]
## Path to PROJ shared files: /Library/Frameworks/R.framework/Versions/4.1/Resources/library/rgdal/proj
## PROJ CDN enabled: FALSE
## Linking to sp version:1.4-6
## To mute warnings of possible GDAL/OSR exportToProj4() degradation,
## use options("rgdal_show_exportToProj4_warnings"="none") before loading sp or rgdal.
## Overwritten PROJ_LIB was /Library/Frameworks/R.framework/Versions/4.1/Resources/library/rgdal/proj
library(tmap)
## Warning: package 'tmap' was built under R version 4.1.2
library(ggmap)
## Google's Terms of Service: https://cloud.google.com/maps-platform/terms/.
## Please cite ggmap if you use it! See citation("ggmap") for details.
## 
## Attaching package: 'ggmap'
## The following object is masked from 'package:plotly':
## 
##     wind
library(RColorBrewer)

import datasets

total <- read.csv("/Users/yuwang/Documents/GitHub/gr5063/Group_A_COVID-19_insights/umployment rate/SeriesReport-20220423235636_21d8c2.csv", stringsAsFactors = FALSE)
states <- read.csv("/Users/yuwang/Documents/GitHub/gr5063/Group_A_COVID-19_insights/umployment rate/unemployment by states.csv", stringsAsFactors = FALSE)
industry <- read.csv("/Users/yuwang/Documents/GitHub/gr5063/Group_A_COVID-19_insights/umployment rate/Unempolyment rate by industry.csv", stringsAsFactors = FALSE)

The first visualization of unemployment rate is a A line chart to capture changes in average national unemployment rates over time.

total1 <- total %>% 
  gather("id", "value", 2:13) %>%
  filter(id == "Jun"|id == "Dec", Year >= 2016 & Year != 2022)

total1$month <- match(total1[, 2], month.abb)

total1$Date <- paste(total1$Year,total1$month)
total1$Date <- ym(total1$Date)
plot1 <- ggplot(total1, aes(Date, value)) +
  geom_point() +
  geom_line( color="#69b3a2") +
  theme_bw()+
  labs(x="Year", y="Unemployment Rate %", title="US Unemployment Rate Over Time ")+
  theme(plot.title=element_text(hjust = 0.5))

ggplotly(plot1)

The second visualization of unemployment rate is shows trend of different industries

ind <- industry%>% 
  gather("id", "value", 3:6)

ind1 = subset(ind, select = -c(3:6))

ind1$Date <- paste(ind1$Year,ind1$id)

ind1$Date <- yq(ind1$Date)
plot2 <- ggplot(ind1, aes(Date, value, col = Industry)) +             # Create ggplot2 plot
  geom_line()+
  facet_wrap(Industry ~., nrow=5) +
  labs(x="Year", y="Unemployment Rate %", title="US Unemployment Rate by Industry Over Time ")+
  theme(plot.title=element_text(hjust = 0.5))
 
ggplotly(plot2)

The third visualization of unemployment rate is an interactive map of US states

colnames(states)[2] <- 'Rate_2019'
colnames(states)[3] <- 'Rate_2020'
colnames(states)[4] <- 'Rate_2021'
geom <- read.csv("/Users/yuwang/Documents/GitHub/gr5063/Group_A_COVID-19_insights/umployment rate/statelatlong.csv")

colnames(geom)[4] <- 'State'

geom = subset(geom, select = -c(1))
statesgeom <- states %>%
  left_join(geom, by = 'State')
usstates <- 
    geojsonio::geojson_read( 
        x = "https://raw.githubusercontent.com/PublicaMundi/MappingAPI/master/data/geojson/us-states.json"
        , what = "sp"
    )
## Registered S3 method overwritten by 'geojsonsf':
##   method        from   
##   print.geojson geojson
class(usstates)
## [1] "SpatialPolygonsDataFrame"
## attr(,"package")
## [1] "sp"
library(RColorBrewer)

pal3 = colorFactor("Set1", domain = states$State)
color_offsel3 = pal3(states$State)
## Warning in RColorBrewer::brewer.pal(max(3, n), palette): n too large, allowed maximum for palette Set1 is 9
## Returning the palette you asked for with that many colors
content3 <- paste("unemployment Rate:",statesgeom$Rate_2019,"<br/>",
                  "State:",statesgeom$State,"<br/>")

content4 <- paste("unemployment Rate:",statesgeom$Rate_2020,"<br/>",
                  "State",statesgeom$State,"<br/>")

content5 <- paste("unemployment Rate:",statesgeom$Rate_2021,"<br/>",
                  "State",statesgeom$State,"<br/>")

leaflet(data=statesgeom) %>%
  setView(-96, 37.8, 4) %>%
  addTiles(group = "OSM") %>%
  addPolygons(data=usstates,group = '2019',
              stroke = TRUE, smoothFactor = 0.5,
              weight=2,  opacity=1,
              color = 'white',
              popup = content3) %>%
  addPolygons(data=usstates,group = '2020',
              stroke = TRUE, smoothFactor = 0.5,
              weight=2,  opacity=1,
              color = 'orange',
              popup = content4) %>%
    addPolygons(data=usstates,group = '2021',
              stroke = TRUE, smoothFactor = 0.5,
              weight=2,  opacity=1,
              color = 'blue',
              popup = content5) %>%
  addLayersControl( #<<
    baseGroups = c("OpenStreetMap"), #<<
    overlayGroups = c("2019",'2020','2021'), #<<
    options = layersControlOptions(collapsed = TRUE) ) %>%
  addLegend(pal = pal3, values = ~statesgeom$State, title = "US States")
## Warning in RColorBrewer::brewer.pal(max(3, n), palette): n too large, allowed maximum for palette Set1 is 9
## Returning the palette you asked for with that many colors